<template>
    <div style="overflow-y:hidden;" class="layersty">
        <div class="tite_btn">
            <div class="font_btn" v-if="config.showName">{{ config.name }}</div>
        </div>
        <FormBoListPreviewList ref="boList" :html="html" :customVm="getCurVm"></FormBoListPreviewList>
    </div>
</template>

<script>
import FormBoListPreviewList from "../FormBoListPreviewList";
import FormBoListApi from "@/api/form/core/formBoList";
import PublicApi from "@/api/form/core/public";
import BusEvent from "@/views/modules/form/core/formComponent/BusEvent";

export default {
    name: "tree-component",
    components: {FormBoListPreviewList},
    mixins:[BusEvent],
    props: {
        config: {
            type: Object,
            default: () => {
                return {}
            }
        },
        layoutItem: {
            type: Object,
            default: () => {
                return {}
            }
        },
    },
    provide(){
        return{
            defaultValue: this.config.defaultValue || "",
            eventFunc:this.eventFunc
        }
    },

    data() {
        return {
            currentView: {
                template: "<div>"+this.$ti18('rxCommon.Loading','正在加载...')+"</div>"
            },
            html: ""
        }
    },
    created() {
        var self_ = this;
        this.$bus.on('refreshEvent', (args) => {
            self_.$refs.boList.loadData();
        })
    },
    mounted() {
        this.loadList();
    },
    methods: {
        getCurVm() {
            return this;
        },
        getData(hasParent) {
            return this.$refs.boList.$refs.current.$refs.rxTree.getCheckedNodes(hasParent);
        },
        getParent(name) {
            var vmObj=this;
            while(!vmObj.isFormCustom){
                vmObj=vmObj.$parent;
            }
            var ctlObj=vmObj.$refs[name];
            if(!ctlObj){
                var layAry=vmObj.$refs;
                for(var lay in layAry){
                    for(var i=0;i<layAry[lay].length;i++){
                        var layObj=layAry[lay][i].$refs[name];
                        if(layObj){
                            return layObj;
                        }
                    }
                }
            }
            return ctlObj;
        },
        loadList() {
            if (!this.config.alias) {
                return;
            }
            var parameter = {key: this.config.alias, params: ''};
            //取路由参数
            var params=this.getRouterParams();
            //读取配置的参数
            for (var i = 0; i < this.config.receive.mapping.length; i++) {
                var o = this.config.receive.mapping[i];
                if(o.valueSource == 'param'){
                    if(this.config.receive.type== "url"){
                        if (params[o.valueDef]) {
                            parameter.params += o.name + '=' + params[o.valueDef] + '&';
                        }
                    }else {
                        if (params[o.name]) {
                            parameter.params += o.name + '=' + params[o.name] + '&';
                        }
                    }
                }
            }
            var self = this;
            this.getParams(function (params) {
                for (var key in params) {
                    parameter.params += key + '=' + params[key] + '&';
                }
                parameter.params = parameter.params.substring(0, parameter.params.length - 1);
                FormBoListApi.dialog(parameter).then(res => {
                    if(!res.success){
                        return;
                    }
                    var record = res.data;
                    self.html = record.listHtml;
                });
            });
        },
        handReceive(args) {
            var receive = this.config.receive;
            //
            if (this.config.receive.type == 'event' && receive.publishComponent == args.component) {
                var inputParams = args.params;
                var params = {};
                //读取配置的参数
                for (var i = 0; i < receive.mapping.length; i++) {
                    var o = receive.mapping[i];
                    //处理没有配置值来源的情况。
                    if(!o.valueDef && inputParams[o.name]){
                        params[o.name]=inputParams[o.name]
                    }
                    else if (o.valueSource == 'param' && inputParams[o.valueDef]) {
                        params[o.name] = inputParams[o.valueDef];
                    }
                }
                this.getParams(function (data,self_) {
                    for (var key in data) {
                        params[key] = data[key];
                    }
                    self_.$refs.boList.loadByParams(params);
                })
            }
        },
        getParams(callback) {
            var self = this;
            PublicApi.getParams(JSON.stringify(this.config.receive.mapping)).then(res => {
                callback(res,self);
            }).catch(err => {
                callback({},self);
            })
        },
        eventFunc(obj){
            if(!obj){
                return;
            }
            var publishConf=this.config.publish.paramMapping;
            if(!publishConf){
                return;
            }
            var params={};
            for (var i = 0; i < publishConf.length; i++) {
                var conf = publishConf[i];
                params[conf.key] = obj[conf.key]
            }
            this.$bus.emit('formEvent', {component: this.config.alias,params:params}) ;
        },
        /**
         * 处理页面定制公共事件
         *
         * @param args
         * {
         *     //方法数据
         *     method:"",
         *     //数据
         *     data:{},
         *     //查询参数:配置参数使用
         *     params:[]
         * }
         */
        handPublicReceive(args){
            //如果调用自定义的方法，则进行调用。
            if(args.method){
                this.$refs.boList.executeMethod(args.method,args.data);
                return ;
            }
            var receive = this.config.receive;
            var inputParams = args.params;
            var params = {};
            //读取配置的参数
            for (var i = 0; i < receive.mapping.length; i++) {
                var o = receive.mapping[i];
                params[o.name]=inputParams[o.name]
            }
            this.getParams(function (data,self_) {
                if(!self_.$refs.boList){
                   return;
                }
                for (var key in data) {
                    params[key] = data[key];
                }
                self_.$refs.boList.loadByParams(params);
            })
        }
    },
    watch: {
        config: {
            handler: function (val, oldVal) {
                if (val) {
                    this.loadList();
                }
            },
            deep: true
        }
    }
}
</script>

<style scoped>
.tite_btn {
    /*position: absolute;*/
    /*z-index: 99;*/
    /*width: 100%;*/
    /*top: 10px;*/
}

.font_btn {
    background: #fafafa;
    padding: 5px 10px;
    border-bottom: 1px solid #dadde0;
    font-size: 14px;
}

.hr_btn {
    /*position: relative;*/
    bottom: -34px;
    z-index: 99;
    margin: 0;
    opacity: .4;

}

.ant-tree {
    margin-bottom: 2vw !important;
}

.layersty {
    height: 100%;
    background: #fff;
    border: 1px solid #dadde0;
}
</style>